package com.test.com.bluetoothchatandroid.base;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

/**
 * description  deal with FragmentTabHost cash problem
 */

public abstract class BaseFragment extends Fragment {
	protected LayoutInflater inflater;
	private View contentView;
	private ViewGroup container;
	private Context context;

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.context=context;
	}

	/**
	 * onCreateView的生命周期是怎样的
	 */
	@Override
	public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater=inflater;
		this.container=container;
		if (contentView == null) {
			setContentView();
		}else {
			ViewGroup parent = (ViewGroup) contentView.getParent();
			if (parent!=null){
				parent.removeView(contentView);
			}
		}
		return contentView;
	}

	public abstract void setContentView();

	public void setContentView(int layoutResID) {
		setContentView(inflater.inflate(layoutResID, container, false));
	}

	public void setContentView(View view) {
		contentView = view;
	}

	public View getContentView() {
		return contentView;
	}


	public void showToast(String msg){
		Toast.makeText(getActivity(),msg, Toast.LENGTH_SHORT).show();
	}

	protected Context getApplicationContext(){
		return context;
	}
	public View findViewById(int id) {
		if (contentView != null)
			return contentView.findViewById(id);
		return null;
	}
}
